﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Pheonix.CodeLibrary.Web;

public partial class control_common_DataPager : System.Web.UI.UserControl
{
    private int _pageSize;
    public int PageSize
    {
        get { return _pageSize; }
        set { _pageSize = value; }
    }

    private int _maxPageLinks;
    public int MaxPageLinks
    {
        get { return _maxPageLinks; }
        set { _maxPageLinks = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //firstPage.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/first.jpg";
        //prevPage.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/prev.jpg";
        //nextPage.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/next.jpg";
        //lastPage.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/last.jpg";
        firstPage.Text = "<<";
        prevPage.Text = "<";
        nextPage.Text = ">";
        lastPage.Text = ">>";
    }

    public void ChangePage(object sender, CommandEventArgs e)
    {
        CurrentPage = Int32.Parse(e.CommandArgument.ToString());
    }

    public void SetPageLinks()
    {
        int startIndex = CurrentPage;
        int endIndex = LastPage;
        int pageSize = _pageSize;

        startIndex = (CurrentPage / (pageSize + 1)) * pageSize + 1;
        //endIndex = (CurrentPage / (pageSize + 1)) * pageSize + pageSize;
        if (LastPage - CurrentPage + 1 >= _maxPageLinks)
            endIndex = startIndex + _maxPageLinks - 1;

        DataTable dataTable = new DataTable();
        DataColumn column = new DataColumn("pageIndex", Type.GetType("System.String"));
        dataTable.Columns.Add(column);
        
        for (int i = startIndex; i <= endIndex; i++)
        {
            DataRow row = dataTable.NewRow();
            row["pageIndex"] = i.ToString();
            dataTable.Rows.Add(row);
        }

        pageLinks.DataSource = dataTable;
        pageLinks.DataBind();

    }

    public void firstPage_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
    }

    public void prevPage_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
    }

    public void nextPage_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
    }

    public void lastPage_Click(object sender, EventArgs e)
    {
        CurrentPage = LastPage;
    }

    public bool PrevPageEnabled
    {
        get { return prevPage.Enabled; }
        set { prevPage.Enabled = value; }
    }

    public bool NextPageEnabled
    {
        get { return nextPage.Enabled; }
        set { nextPage.Enabled = value; }
    }

    public int CurrentPage
    {
        get
        {
            // look for current page in session
            int? currentPage = SessionManager.GetPagerCurrentPage(this.UniqueID);

            if (!currentPage.HasValue)
                return 1; // default page index of 1
            else
                return currentPage.Value;
        }

        set
        {
            if (value <= 0)
                SessionManager.SetPagerCurrentPage(this.UniqueID, 1);
            else
                SessionManager.SetPagerCurrentPage(this.UniqueID, value);
        }
    }

    public int LastPage
    {
        get
        {
            // look for last page in session
            int? lastPage = SessionManager.GetPagerLastPage(this.UniqueID);
            if (!lastPage.HasValue)
                return 1; // default page index of 1
            else
                return lastPage.Value;
        }

        set
        {
            if (value <= 0)
                SessionManager.SetPagerLastPage(this.UniqueID,1);
            else
                SessionManager.SetPagerLastPage(this.UniqueID,value);
        }
    }
}
